package com.mxpio.erp.equipment.controller;

import com.mxpio.erp.common.equipment.entity.EquipmentAuditTransfer;
import com.mxpio.erp.common.equipment.service.EquipmentAuditTransferService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;


@Tag(name = "EquipmentAuditTransferController}", description = "稽核转单历史")
@RestController
@RequestMapping("/erp/equipment/audit/transfer/")
public class EquipmentAuditTransferController {

    @Autowired
    private EquipmentAuditTransferService equipmentAuditTransferService;


    @GetMapping("list")
    @Operation(summary = "稽核转单历史列表", description = "获取稽核转单历史列表（无分页）", method = "GET")
    public Result<List<EquipmentAuditTransfer>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<EquipmentAuditTransfer> list = equipmentAuditTransferService.list(EquipmentAuditTransfer.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "稽核转单历史列表", description = "获取稽核转单历史列表（分页）", method = "GET")
    public Result<Page<EquipmentAuditTransfer>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<EquipmentAuditTransfer> pageResult = equipmentAuditTransferService.listPage(EquipmentAuditTransfer.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{id}")
    @Operation(summary = "根据ID获取稽核转单历史列表", description = "根据ID获取稽核转单历史列表", method = "GET")
    public Result<EquipmentAuditTransfer> getById(@PathVariable(name = "id", required = true) String id) {
        EquipmentAuditTransfer equipmentAuditTransfer = equipmentAuditTransferService.getById(EquipmentAuditTransfer.class, id);
        return Result.OK(equipmentAuditTransfer);
    }

    @RequestMapping("add")
    @Operation(summary = "保存稽核转单历史", description = "保存稽核转单历史")
    public Result<EquipmentAuditTransfer> add(@RequestBody EquipmentAuditTransfer equipmentAuditTransfer) {
        equipmentAuditTransferService.save(equipmentAuditTransfer,new SmartCrudPolicyAdapter(){});
        return Result.OK(equipmentAuditTransfer);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑稽核转单历史", description = "编辑稽核转单历史", method = "PUT")
    public Result<EquipmentAuditTransfer> edit(@RequestBody EquipmentAuditTransfer equipmentAuditTransfer) {
        equipmentAuditTransferService.update(equipmentAuditTransfer);
        return Result.OK(equipmentAuditTransfer);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除稽核转单历史", description = "删除稽核转单历史,多个id用英文逗号分隔", method = "DELETE")
    public Result<EquipmentAuditTransfer> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            equipmentAuditTransferService.delete(EquipmentAuditTransfer.class, key);
        }
        return Result.OK();
    }


}
